package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.Result;
import com.example.reggie.domain.Employee;
import com.example.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController
{
    @Autowired
    EmployeeService employeeService;


    // 员工登录接口
    @PostMapping("/login")
    Result<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lambdaQueryWrapper);
        if (emp==null){
            return Result.error("账号不存在");
        }

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(emp.getPassword())){
            return Result.error("账号或密码错误");
        }

        if (emp.getStatus()!=1){
            return Result.error("账号已封禁");
        }
        request.getSession().setAttribute("employee",emp.getId());

        log.info(emp.toString());
        return Result.success(emp);
    }


    // 员工登出接口
    @PostMapping("/logout")
    Result<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return Result.success("退出成功");
    }

    // 员工列表分页查询
    @GetMapping("/page")
    Result<Page> employeePageList(Integer page,Integer pageSize,String name){
        // 构造分页构造器
        Page<Employee> employeePage = new Page<>(page,pageSize);
        // 构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.like(name!=null,Employee::getName,name);
        // 添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(employeePage,queryWrapper);
        return Result.success(employeePage);
    }

    // 员工添加接口
    @PostMapping
    Result<String> employeeAdd(@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return Result.success("添加员工成功");
    }

    // 员工修改接口
    @PutMapping
    Result<String> employeeUpdate(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return Result.success("修改成功");
    }

    // 通过id查询员工
    @GetMapping("/{id}")
    Result<Employee> employeeGetById(@PathVariable long id){
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

}
